package prosjekt;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.*;

public class PrintUI implements Printable, ActionListener {

	String tekst;
	String[] tmp;
	int y;
	
	public PrintUI(String tekst) {
		this.tekst = tekst;
	}
	
	public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
		if (page > 0) {
            return NO_SUCH_PAGE;
        }

        Graphics2D g2d = (Graphics2D)g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());
        

        tmp = tekst.split("\n");
    	for (int i = 0; i < tmp.length; i++) {
            	g.drawString(tmp[i], 100, y);
    		y += 10;
    	}

//      g.drawString(tekst, 100, 100);


        return PAGE_EXISTS;
	}
	
	public void actionPerformed(ActionEvent e) {
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(this);
        boolean ok = job.printDialog();
        if (ok) {
            try {
                 job.print();
            } catch (PrinterException ex) {
             /* The job did not successfully complete */
            }
        }
   }

}
